package com.huawei.hms.framework.network.restclient;

import com.huawei.hms.framework.common.CheckParamUtils;
import com.huawei.hwid.core.constants.HwAccountConstants;
import java.lang.reflect.Array;
import java.lang.reflect.GenericArrayType;
import java.lang.reflect.GenericDeclaration;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.lang.reflect.TypeVariable;
import java.lang.reflect.WildcardType;
import java.util.Arrays;
import java.util.NoSuchElementException;
import javax.annotation.Nullable;

public class Utils {
    static final Type[] EMPTY_TYPE_ARRAY = new Type[0];

    static final class GenericArrayTypeImpl implements GenericArrayType {
        private final Type componentType;

        GenericArrayTypeImpl(Type type) {
            this.componentType = type;
        }

        public Type getGenericComponentType() {
            return this.componentType;
        }

        public boolean equals(Object obj) {
            return (obj instanceof GenericArrayType) && Utils.equals((Type) this, (Type) (GenericArrayType) obj);
        }

        public int hashCode() {
            return this.componentType.hashCode();
        }

        public String toString() {
            return Utils.typeToString(this.componentType) + "[]";
        }
    }

    static final class ParameterizedTypeImpl implements ParameterizedType {
        private final Type ownerType;
        private final Type rawType;
        private final Type[] typeArguments;

        ParameterizedTypeImpl(@Nullable Type type, Type type2, Type... typeArr) {
            int i = 1;
            int i2 = 0;
            if (type2 instanceof Class) {
                int i3;
                if (type == null) {
                    i3 = 1;
                } else {
                    i3 = 0;
                }
                if (((Class) type2).getEnclosingClass() != null) {
                    i = 0;
                }
                if (i3 != i) {
                    throw new IllegalArgumentException();
                }
            }
            int length = typeArr.length;
            while (i2 < length) {
                Type type3 = typeArr[i2];
                CheckParamUtils.checkNotNull(type3, "typeArgument == null");
                Utils.checkNotPrimitive(type3);
                i2++;
            }
            this.ownerType = type;
            this.rawType = type2;
            this.typeArguments = (Type[]) typeArr.clone();
        }

        public Type[] getActualTypeArguments() {
            return (Type[]) this.typeArguments.clone();
        }

        public Type getRawType() {
            return this.rawType;
        }

        public Type getOwnerType() {
            return this.ownerType;
        }

        public boolean equals(Object obj) {
            return (obj instanceof ParameterizedType) && Utils.equals((Type) this, (Type) (ParameterizedType) obj);
        }

        public int hashCode() {
            return (this.ownerType != null ? this.ownerType.hashCode() : 0) ^ (this.rawType.hashCode() ^ Arrays.hashCode(this.typeArguments));
        }

        public String toString() {
            if (this.typeArguments.length == 0) {
                return Utils.typeToString(this.rawType);
            }
            StringBuilder stringBuilder = new StringBuilder((this.typeArguments.length + 1) * 30);
            stringBuilder.append(Utils.typeToString(this.rawType));
            stringBuilder.append("<").append(Utils.typeToString(this.typeArguments[0]));
            for (int i = 1; i < this.typeArguments.length; i++) {
                stringBuilder.append(", ").append(Utils.typeToString(this.typeArguments[i]));
            }
            return stringBuilder.append(">").toString();
        }
    }

    static final class WildcardTypeImpl implements WildcardType {
        private final Type lowerBoundType;
        private final Type upperBoundType;

        WildcardTypeImpl(Type[] typeArr, Type[] typeArr2) {
            if (typeArr2.length > 1 || typeArr.length != 1) {
                throw new IllegalArgumentException();
            } else if (typeArr2.length == 1) {
                if (typeArr2[0] == null) {
                    throw new NullPointerException();
                }
                Utils.checkNotPrimitive(typeArr2[0]);
                if (typeArr[0] != Object.class) {
                    throw new IllegalArgumentException();
                }
                this.lowerBoundType = typeArr2[0];
                this.upperBoundType = Object.class;
            } else if (typeArr[0] == null) {
                throw new NullPointerException();
            } else {
                Utils.checkNotPrimitive(typeArr[0]);
                this.lowerBoundType = null;
                this.upperBoundType = typeArr[0];
            }
        }

        public Type[] getUpperBounds() {
            return new Type[]{this.upperBoundType};
        }

        public Type[] getLowerBounds() {
            if (this.lowerBoundType == null) {
                return Utils.EMPTY_TYPE_ARRAY;
            }
            return new Type[]{this.lowerBoundType};
        }

        public boolean equals(Object obj) {
            return (obj instanceof WildcardType) && Utils.equals((Type) this, (Type) (WildcardType) obj);
        }

        public int hashCode() {
            return (this.lowerBoundType != null ? this.lowerBoundType.hashCode() + 31 : 1) ^ (this.upperBoundType.hashCode() + 31);
        }

        public String toString() {
            if (this.lowerBoundType != null) {
                return "? super " + Utils.typeToString(this.lowerBoundType);
            }
            if (this.upperBoundType == Object.class) {
                return "?";
            }
            return "? extends " + Utils.typeToString(this.upperBoundType);
        }
    }

    private static boolean equals(Object obj, Object obj2) {
        return obj == obj2 || (obj != null && obj.equals(obj2));
    }

    private static boolean equals(Type type, Type type2) {
        if (type == type2) {
            return true;
        }
        if (type instanceof Class) {
            return type.equals(type2);
        }
        return interfaceTypeEquals(type, type2);
    }

    public static Type getSubmitResponseType(Type type) {
        if (type instanceof ParameterizedType) {
            return getParameterUpperBound(0, (ParameterizedType) type);
        }
        throw new IllegalArgumentException("Submit return type must be parameterized as Submit<Foo> or Submit<? extends Foo>");
    }

    private static boolean interfaceTypeEquals(Type type, Type type2) {
        boolean z = true;
        if (type instanceof ParameterizedType) {
            if (!(type2 instanceof ParameterizedType)) {
                return false;
            }
            ParameterizedType parameterizedType = (ParameterizedType) type;
            ParameterizedType parameterizedType2 = (ParameterizedType) type2;
            if (!(equals(parameterizedType.getOwnerType(), parameterizedType2.getOwnerType()) && equals(parameterizedType.getRawType(), parameterizedType2.getRawType()) && Arrays.equals(parameterizedType.getActualTypeArguments(), parameterizedType2.getActualTypeArguments()))) {
                z = false;
            }
            return z;
        } else if (type instanceof GenericArrayType) {
            if (!(type2 instanceof GenericArrayType)) {
                return false;
            }
            return equals(((GenericArrayType) type).getGenericComponentType(), ((GenericArrayType) type2).getGenericComponentType());
        } else if (type instanceof WildcardType) {
            if (!(type2 instanceof WildcardType)) {
                return false;
            }
            WildcardType wildcardType = (WildcardType) type;
            WildcardType wildcardType2 = (WildcardType) type2;
            if (!(Arrays.equals(wildcardType.getUpperBounds(), wildcardType2.getUpperBounds()) && Arrays.equals(wildcardType.getLowerBounds(), wildcardType2.getLowerBounds()))) {
                z = false;
            }
            return z;
        } else if (!(type instanceof TypeVariable) || !(type2 instanceof TypeVariable)) {
            return false;
        } else {
            TypeVariable typeVariable = (TypeVariable) type;
            TypeVariable typeVariable2 = (TypeVariable) type2;
            if (!(typeVariable.getGenericDeclaration() == typeVariable2.getGenericDeclaration() && typeVariable.getName().equals(typeVariable2.getName()))) {
                z = false;
            }
            return z;
        }
    }

    public static boolean hasUnresolvableType(Type type) {
        if (type instanceof Class) {
            return false;
        }
        if (type instanceof ParameterizedType) {
            for (Type hasUnresolvableType : ((ParameterizedType) type).getActualTypeArguments()) {
                if (hasUnresolvableType(hasUnresolvableType)) {
                    return true;
                }
            }
            return false;
        } else if (type instanceof GenericArrayType) {
            return hasUnresolvableType(((GenericArrayType) type).getGenericComponentType());
        } else {
            if ((type instanceof TypeVariable) || (type instanceof WildcardType)) {
                return true;
            }
            throw new IllegalArgumentException("Expected a Class, ParameterizedType, or GenericArrayType, but <" + type + "> is of type " + (type == null ? HwAccountConstants.NULL : type.getClass().getName()));
        }
    }

    public static Class<?> getRawType(Type type) {
        CheckParamUtils.checkNotNull(type, "type == null");
        if (type instanceof Class) {
            return (Class) type;
        }
        if (type instanceof ParameterizedType) {
            Type rawType = ((ParameterizedType) type).getRawType();
            if (rawType instanceof Class) {
                return (Class) rawType;
            }
            throw new IllegalArgumentException();
        } else if (type instanceof GenericArrayType) {
            return Array.newInstance(getRawType(((GenericArrayType) type).getGenericComponentType()), 0).getClass();
        } else {
            if (type instanceof TypeVariable) {
                return Object.class;
            }
            if (type instanceof WildcardType) {
                return getRawType(((WildcardType) type).getUpperBounds()[0]);
            }
            throw new IllegalArgumentException("Expected a Class, ParameterizedType, or GenericArrayType, but <" + type + "> is of type " + type.getClass().getName());
        }
    }

    public static Type getParameterUpperBound(int i, ParameterizedType parameterizedType) {
        Type[] actualTypeArguments = parameterizedType.getActualTypeArguments();
        if (i < 0 || i >= actualTypeArguments.length) {
            throw new IllegalArgumentException("Index " + i + " not in range [0," + actualTypeArguments.length + ") for " + parameterizedType);
        }
        Type type = actualTypeArguments[i];
        if (type instanceof WildcardType) {
            return ((WildcardType) type).getUpperBounds()[0];
        }
        return type;
    }

    static Type getGenericSupertype(Type type, Class<?> cls, Class<?> cls2) {
        if (cls2 == cls) {
            return type;
        }
        if (cls2.isInterface()) {
            Class[] interfaces = cls.getInterfaces();
            int length = interfaces.length;
            for (int i = 0; i < length; i++) {
                if (interfaces[i] == cls2) {
                    return cls.getGenericInterfaces()[i];
                }
                if (cls2.isAssignableFrom(interfaces[i])) {
                    return getGenericSupertype(cls.getGenericInterfaces()[i], interfaces[i], cls2);
                }
            }
        }
        if (!cls.isInterface()) {
            while (cls != Object.class) {
                Class<?> superclass = cls.getSuperclass();
                if (superclass == cls2) {
                    return cls.getGenericSuperclass();
                }
                if (cls2.isAssignableFrom(superclass)) {
                    return getGenericSupertype(cls.getGenericSuperclass(), superclass, cls2);
                }
                cls = superclass;
            }
        }
        return cls2;
    }

    public static Type getSupertype(Type type, Class<?> cls, Class<?> cls2) {
        if (cls2.isAssignableFrom(cls)) {
            return resolve(type, cls, getGenericSupertype(type, cls, cls2));
        }
        throw new IllegalArgumentException();
    }

    static Type resolve(Type type, Class<?> cls, Type type2) {
        Type resolveTypeVariable;
        if (type2 instanceof TypeVariable) {
            type2 = (TypeVariable) type2;
            resolveTypeVariable = resolveTypeVariable(type, cls, type2);
            if (resolveTypeVariable == type2) {
                return resolveTypeVariable;
            }
            return resolve(type, cls, resolveTypeVariable);
        } else if ((type2 instanceof Class) && ((Class) type2).isArray()) {
            Class cls2 = (Class) type2;
            r1 = cls2.getComponentType();
            if (r1 == null) {
                return type2;
            }
            Type resolve = resolve(type, cls, r1);
            if (r1 != resolve) {
                cls2 = new GenericArrayTypeImpl(resolve);
            }
            return cls2;
        } else if (type2 instanceof GenericArrayType) {
            GenericArrayType genericArrayType = (GenericArrayType) type2;
            resolveTypeVariable = genericArrayType.getGenericComponentType();
            r1 = resolve(type, cls, resolveTypeVariable);
            if (resolveTypeVariable != r1) {
                return new GenericArrayTypeImpl(r1);
            }
            return genericArrayType;
        } else if (type2 instanceof ParameterizedType) {
            return resolveParameterizedType(type, cls, type2);
        } else {
            if (type2 instanceof WildcardType) {
                return resolveWildcardType(type, cls, type2);
            }
            return type2;
        }
    }

    static Type resolveParameterizedType(Type type, Class<?> cls, Type type2) {
        Object obj;
        ParameterizedType parameterizedType = (ParameterizedType) type2;
        Type ownerType = parameterizedType.getOwnerType();
        Type resolve = resolve(type, cls, ownerType);
        if (resolve != ownerType) {
            obj = 1;
        } else {
            obj = null;
        }
        Type[] actualTypeArguments = parameterizedType.getActualTypeArguments();
        int length = actualTypeArguments.length;
        Object obj2 = obj;
        Type[] typeArr = actualTypeArguments;
        for (int i = 0; i < length; i++) {
            Type resolve2 = resolve(type, cls, typeArr[i]);
            if (resolve2 != typeArr[i]) {
                if (obj2 == null) {
                    typeArr = (Type[]) typeArr.clone();
                    obj2 = 1;
                }
                typeArr[i] = resolve2;
            }
        }
        return obj2 != null ? new ParameterizedTypeImpl(resolve, parameterizedType.getRawType(), typeArr) : parameterizedType;
    }

    static Type resolveWildcardType(Type type, Class<?> cls, Type type2) {
        WildcardType wildcardType = (WildcardType) type2;
        Type[] lowerBounds = wildcardType.getLowerBounds();
        Type[] upperBounds = wildcardType.getUpperBounds();
        if (lowerBounds.length == 1) {
            if (resolve(type, cls, lowerBounds[0]) == lowerBounds[0]) {
                return wildcardType;
            }
            return new WildcardTypeImpl(new Type[]{Object.class}, new Type[]{r1});
        } else if (upperBounds.length != 1 || resolve(type, cls, upperBounds[0]) == upperBounds[0]) {
            return wildcardType;
        } else {
            return new WildcardTypeImpl(new Type[]{resolve(type, cls, upperBounds[0])}, EMPTY_TYPE_ARRAY);
        }
    }

    private static Type resolveTypeVariable(Type type, Class<?> cls, TypeVariable<?> typeVariable) {
        GenericDeclaration genericDeclaration = typeVariable.getGenericDeclaration();
        Class cls2 = genericDeclaration instanceof Class ? (Class) genericDeclaration : null;
        if (cls2 == null) {
            return typeVariable;
        }
        Type genericSupertype = getGenericSupertype(type, cls, cls2);
        if (!(genericSupertype instanceof ParameterizedType)) {
            return typeVariable;
        }
        return ((ParameterizedType) genericSupertype).getActualTypeArguments()[indexOfArray(cls2.getTypeParameters(), typeVariable)];
    }

    private static int indexOfArray(Object[] objArr, Object obj) {
        for (int i = 0; i < objArr.length; i++) {
            if (obj.equals(objArr[i])) {
                return i;
            }
        }
        throw new NoSuchElementException();
    }

    static String typeToString(Type type) {
        return type instanceof Class ? ((Class) type).getName() : type.toString();
    }

    static void checkNotPrimitive(Type type) {
        if ((type instanceof Class) && ((Class) type).isPrimitive()) {
            throw new IllegalArgumentException();
        }
    }

    public static Class<?> boxIfPrimitive(Class<?> cls) {
        if (Boolean.TYPE == cls) {
            return Boolean.class;
        }
        if (Byte.TYPE == cls) {
            return Byte.class;
        }
        if (Character.TYPE == cls) {
            return Character.class;
        }
        if (Double.TYPE == cls) {
            return Double.class;
        }
        if (Float.TYPE == cls) {
            return Float.class;
        }
        if (Integer.TYPE == cls) {
            return Integer.class;
        }
        if (Long.TYPE == cls) {
            return Long.class;
        }
        if (Short.TYPE == cls) {
            return Short.class;
        }
        return cls;
    }
}
